var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var userUtil = require("helper/user");
var activityUrl = require("url/activity");
var detailUrl = activityUrl.url.base.detail;
var qqHttp = require("helper/http");
var rootview = ui("$");
var shareHelper = require("helper/share");

var pubUrl = require("url/pub");
var bizzType = require("helper/bizzType");
var openHelper = require("helper/open");
var httpSilence = require("helper/httpSilence");
//UI
var alayoutRoot = ui("alayoutRoot");
var headerAout = ui("headerAout");
var listViewDetail = ui("listViewDetail");

var canDeleteActivity = false;//是否可以删除话题
var pageNum = 1;//当前页码
var isLastPage = false;//是否最后一页
var isPraised = 2;
var id;
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
//从open的上层页面返回,刷新数据
do_Page.on("result", function(data){
	if (data.comment_add) {
		loadPage();
	}
});

/****************处理页面-头部-begin*****************/
//左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
//右上角-
var headerRight = ui(addheader + ".headerRight");
headerLeft.on("touch", function() {
	do_App.closePage()
});

var headerRight = ui(addheader + ".headerRight");
//右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "详情"
})
header.refreshData();
/****************处理页面-头部-end*****************/

var footerAout = ui("footerAout");
var addfooter = footerAout.add("footer", "source://view/public/comment/activityBottom.ui", 0, 0);
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	//读取当前页面的传入参数
	var data=do_Page.getData();
	id=data.id;
	loadPage();
});

do_Page.on("sendTextMsg", function(data, e) {
	loadPage();
});
var creatorId;
var detailList = mm("do_ListData");
listViewDetail.bindItems(detailList);
function loadPage(){
	var params = {
		id : id
	};
	qqHttp.post(detailUrl, params, function(data) {
		// 去掉遮盖
		listViewDetail.rebound();
		detailList.removeAll();
		data.detailVo.template = 1;//指定模板
		creatorId=data.detailVo.creatorId;
		detailList.addOne(data.detailVo);
		listViewDetail.refreshItems();
		canDeleteActivity = data.canDeleteActivity;
		bindCommentData();
		var param ={
				activityId:data.detailVo.id,
				status:data.detailVo.status,
				showStatus:data.detailVo.showStatus,
				isSign:data.detailVo.isSign,
				bizzType:bizzType.Level1.ACTIVITY,
				shareTitle:data.detailVo.name,
				shareContent:data.detailVo.resume,
				shareId:data.detailVo.id,
				shareType:shareHelper.type.ACTIVITY
			};
		if(null!=data.detailVo.logo){
			param.shareImage=data.detailVo.pathLogo;
		}
		do_Page.fire("initSign", param);
	});
	
}

//全部刷新数据
function bindCommentData(){
	pageNum=1;
	var params = {
			bizzId:id,
			bizzType:bizzType.Level1.ACTIVITY,
			page:pageNum	
	};
	httpSilence.post(pubUrl.url.comment.list,params,function(data){
		if(data.commentPage){
			isLastPage = data.commentPage.last;//是否最后一页
			if(detailList.getCount()>1){
				//清除已经加载了话题,把话题列表数据清除
				detailList.removeRange(1, detailList.getCount()-1);
			}
			detailList.addData(data.commentPage.content);
			listViewDetail.refreshItems();//刷新item数据
		}
	});
}

//下一页的评论信息
function getNextPageCommentData(){
	if(!isLastPage){
		pageNum++;
		var params = {
				bizzId:id,
				bizzType:bizzType.Level1.ACTIVITY,
				page:pageNum	
		};
		qqHttp.post(pubUrl.url.comment.list,params,function(data){
			if(data.commentPage){
				isLastPage = data.commentPage.last;//是否最后一页
				detailList.addData(data.commentPage.content);
				listViewDetail.refreshItems();
			}
		});
	}
}

//下拉列表，刷新数据
listViewDetail.on("pull", function(data){
	if (data.state == 2){
		loadPage();
		deviceone.print("pull");
		listViewDetail.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listViewDetail.on("push", function(data){
	if (data.state == 2){
		getNextPageCommentData();
		listViewDetail.rebound();//复位【一定要做】
	}
});

/*
 * 菜单弹出
 */
var menus = [];
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	menus = [];
	if(null!=userUtil.userId()&&''!=userUtil.userId()&&creatorId==userUtil.userId()){
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/report.png",
			"menuName" : "举报",
			"type" : "addReport"
		});
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/edit.png",
			"menuName" : "管理",
			"type" : "toManager"
		});
	}else{
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/report.png",
			"menuName" : "举报",
			"type" : "addReport"
		});
	}
	if(canDeleteActivity){
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/del.png",
			"menuName" : "删除活动",
			"type" : "delActivity"
		})
	}
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if (actionType == "addReport") {// 举报
		openHelper.open("source://view/public/report/main.ui",{"bizzId":id,"bizzType":bizzType.Level1.ACTIVITY});
	}else if(actionType == "toManager"){
		openHelper.open("source://view/activity/manage/index.ui");
	}else if(actionType == "delActivity"){
		qqHttp.post(activityUrl.url.base.del,{id:id},function(data){
			nf.toast("删除成功");
			do_App.closePage({activity_delete:true});
		});
	}
});
var addShare = rootview.add("share","source://view/reuse/share.ui", 0, 0);